package com.tx.service.impl;

import com.tx.dao.BlogDao;
import com.tx.dao.CommentDao;
import com.tx.entity.Blog;
import com.tx.entity.Comment;
import com.tx.entity.Page;
import com.tx.entity.Result;
import com.tx.service.BlogService;
import com.tx.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description: 博客管理业务层实现类
 * @Date: 2019/11/5
 **/
@Service
public class BlogServiceImpl implements BlogService {
    //注入博客数据持久层接口
    @Autowired
    private BlogDao blogDao;
    //注入评论数据持久层接口
    @Autowired
    private CommentDao commentDao;

    /* *
     * @description: 新增博客
     * @author tcx
     * @date 2019/11/5
     * @param blog
     * @return void
     */
    public Integer add(Blog blog) {
        //添加博客创建时间
        blog.setbCreateTime(new Date());
        //添加博客
        return blogDao.insert(blog);
    }
    /* *
     * @description: 修改博客
     * @author tcx
     * @date 2019/11/5
     * @param blog
     * @return void
     */
    public Integer edit(Blog blog) {
        //添加博客修改时间
        blog.setbUpdateTime(new Date());
        return blogDao.update(blog);
    }
    /* *
     * @description: 根据id删除博客
     * @author tcx
     * @date 2019/11/5
     * @param bId
     * @return void
     */
    public Integer remove(Integer bId) {
        //删除博客下的评论
        commentDao.deleteBybId(bId);
        //删除博客
       return blogDao.delete(bId);
    }
    /* *
     * @description: 根据id获取博客
     * @author tcx
     * @date 2019/11/5
     * @param bId
     * @return void
     */
    public Blog get(Integer bId) {
       return blogDao.select(bId);
    }
    /* *
     * @description: 获取所有博客
     * @author tcx
     * @date 2019/11/5
     * @param
     * @return java.util.List<com.tx.entity.Blog>
     */
    public List<Blog> getAll() {
        return blogDao.selectAll();
    }
    /* *
     * @description:  根据关键字获取博客列表
     * @author tcx
     * @date 2019/11/5
     * @param key
     * @return java.util.List<com.tx.entity.Blog>
     */
    public Result getAllByKey(Page page,String key) {
        //获得分类查询博客总记录数
        Integer count = blogDao.selectBlogKeyCount(key);
        page = PageUtils.createPage(page,count);
        List<Blog> list = blogDao.selectKey(key,page.getBeginIndex(),page.getEveryPage());
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    /* *
     * @description:  根据用户id获取用户博客,分页查询
     * @author tcx
     * @date 2019/11/10
     * @param page
     * @param uId
     * @return com.tx.entity.Result
     */
    public Result getAllByUser(Page page, Integer uId) {
        //获得用户博客总记录
        Integer count = blogDao.selectByUserCount(uId);
        page = PageUtils.createPage(page,count);
        List<Blog> list = blogDao.selectByUser(uId,page.getBeginIndex(),page.getEveryPage());
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }

    public Integer getByUserCount(Integer uId) {
        return blogDao.selectByUserCount(uId);
    }
    /* *
     * @description:  分页查询所有博客
     * @author tcx
     * @date 2019/11/12
     * @param page
     * @return com.tx.entity.Result
     */
    public Result getBlogByPage(Page page) {
        //获得博客总记录数
        Integer count = blogDao.selectBlogCount();
        page = PageUtils.createPage(page,count);
        List<Blog> list = blogDao.selectBlogByPage(page);
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    /* *
     * @description:  根据博客分类查询博客
     * @author tcx
     * @date 2019/11/13
     * @param page
     * @param tId
     * @return com.tx.entity.Result
     */
    public Result getBlogByTypePage(Page page,Integer tId){
        //获得分类查询博客总记录数
        Integer count = blogDao.selectBlogTypeCount(tId);
        page = PageUtils.createPage(page,count);
        List<Blog> list = blogDao.selectBlogByTypePage(page.getBeginIndex(),page.getEveryPage(),tId);
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    /* *
     * @description: 查询最新发布的十条博客
     * @author tcx
     * @date 2019/11/13
     * @param
     * @return java.util.List<com.tx.entity.Blog>
     */
    public List<Blog> getNew() {
        return blogDao.selectNew();
    }
    /* *
     * @description:  根据博客id查询评论
     * @author tcx
     * @date 2019/11/13
     * @param bId
     * @return java.util.List<com.tx.entity.Comment>
     */
    public List<Comment> getCommentByBlog(Integer bId) {
        return commentDao.selectCommByBlog(bId);
    }
    /* *
     * @description:  添加评论
     * @author tcx
     * @date 2019/11/13
     * @param comment
     * @return void
     */
    public void addComment(Comment comment) {
        //添加评论时间
        comment.setcCreateTime(new Date());
        commentDao.insert(comment);
        /*//根据id查找博客
        Blog blog = new Blog();
        blog.setbCom(commentDao.selectCommByBlogCount(comment.getbId()));
        blog.setbId(comment.getbId());
        //对博客添加评论数量
        blogDao.updateCount(blog);*/
    }
    /* *
     * @description:  查询用户最新十条博客
     * @author tcx
     * @date 2019/11/16
     * @param uId
     * @return java.util.List<com.tx.entity.Blog>
     */
    public List<Blog> getUserNew(Integer uId) {
        return blogDao.selectUserNew(uId);
    }

    public Integer removeComm(Integer cId) {
        return commentDao.delete(cId);
    }

    /* *
     * @description:  查询所有博客总数
     * @author tcx
     * @date 2020/1/13
     * @param
     * @return java.lang.Integer
     */
    public Integer getBlogCount() {
        return blogDao.selectBlogCount();
    }
    public Integer getTotal(Map<String, Object> map) {
        return blogDao.getTotal(map);
    }

    public List<Blog> list(Map<String, Object> map) {
        return blogDao.selectBlogKey(map);
    }

    public List<Comment> getCommentAll(Map<String,Object> map) {
        return commentDao.selectCommAll(map);
    }
    //评论总数
    public Integer getTotalComm(Map<String, Object> map) {
        return commentDao.selectCommAllCount(map);
    }

    public Integer editComm(Comment comment) {
        return commentDao.update(comment);
    }

    //获取用户被评论的总数
    public Integer getComStateCount(Map<String, Object> map) {
        Integer count =commentDao.selectMsgComm(map);
        if(count!=null&&count>0)
            return count;
        return 0;
    }

    public Integer editCommState(Integer cId) {
        return commentDao.updateSate(cId);
    }
}
